﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;

public partial class Main_ShopAdd : System.Web.UI.Page
{
    ShopDao sd = new ShopDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            if (uv.Ugroup == "管理员")
            {
                dt = sd.getField();
                ddlfield.DataSource = dt;
                ddlfield.DataTextField = "Fname";
                ddlfield.DataValueField = "Fid";
                ddlfield.DataBind();
            }
            else
            {
                Response.Redirect("../index.aspx");
            }
        }
    }

    protected void ddlfield_SelectedIndexChanged(object sender, EventArgs e)
    {
        dt = sd.getType(ddlfield.SelectedValue);
        ddltype.DataSource = dt;
        ddltype.DataTextField = "Tname";
        ddltype.DataValueField = "Tid";
        ddltype.DataBind();
    }
    protected void btnuplogo_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "logo", fulogo, Extensions, 50000, "Logo", imglogo);
        string script = "<script>window.alert('" + msg + "');</script>";
        Page.RegisterClientScriptBlock("", script);
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        if (ddlfield.SelectedValue == "0")
        {
            string script = "<script>window.alert('请选择所属领域！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        if (ddltype.SelectedValue == "0")
        {
            string script = "<script>window.alert('请选择所属类型！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        ShopVo sv = new ShopVo();
        sv.Sname = txtname.Text;
        sv.Fid =Convert.ToInt32(ddlfield.SelectedValue);
        sv.Tid = Convert.ToInt32(ddltype.SelectedValue);
        sv.Sintroduce = txtintrduce.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n","<br>");
        sv.Scontent = txtcontent.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
        sv.Sjob = txtjob.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
        sv.Saddress = txtaddress.Text;
        sv.Stel = txttel.Text;
        sv.Slogo = imglogo.ImageUrl;
        sv.Spic = imgtop1.ImageUrl + "|" + imgtop2.ImageUrl + "|" + imgtop3.ImageUrl;
        sv.Shot = Convert.ToInt32(ddlhot.SelectedValue);
        sv.Sstate = 0;
        sv.Uid = 0;
        sv.Slicense = imglicense.ImageUrl;
        sv.Sidcard = imgidcard.ImageUrl;
        Exception ex = sd.addShop(sv);
        if (ex == null)
        {
            string script = "<script>window.alert('添加成功！');location.href='ShopData.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        else
        {
            string script = "<script>window.alert('添加失败！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }
    protected void btnuplicense_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "license", fulicense, Extensions, 200000, "license", imglicense);
        string script = "<script>window.alert('" + msg + "');</script>";
        Page.RegisterClientScriptBlock("", script);
    }
    protected void btnupidcard_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "idcard", fulicense, Extensions, 200000, "idcard", imgidcard);
        string script = "<script>window.alert('" + msg + "');</script>";
        Page.RegisterClientScriptBlock("", script);
    }
    protected void btnuptop1_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", futop1, Extensions, 100000, "top1", imgtop1);
        string script = "<script>window.alert('" + msg + "');</script>";
        Page.RegisterClientScriptBlock("", script);
    }
    protected void btnuptop2_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", futop2, Extensions, 100000, "top2", imgtop2);
        string script = "<script>window.alert('" + msg + "');</script>";
        Page.RegisterClientScriptBlock("", script);
    }
    protected void btnuptop3_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", futop3, Extensions, 100000, "top3", imgtop3);
        string script = "<script>window.alert('" + msg + "');</script>";
        Page.RegisterClientScriptBlock("", script);
    }

    public string UploadImg(string SorH, string type, FileUpload fu, string[] Extensions, int size, string info, Image img)
    {
        bool fileOK = false;
        string path = Server.MapPath("../images/" + SorH + "/" + type + "/") + fu.FileName;
        if (fu.FileName != "")
        {
            if (fu.HasFile)
            {
                string fileExtension = Path.GetExtension(fu.FileName).ToLower();
                string[] allowerExtensions = Extensions;
                for (int i = 0; i < allowerExtensions.Length; i++)
                {
                    if (fileExtension == allowerExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                if (!fileOK)
                {
                    return "图片格式不正确！";
                }
                else
                {
                    if (fu.PostedFile.ContentLength <= size)
                    {
                        string picPath = path.Replace(fu.FileName, txtname.Text + info + fileExtension);
                        fu.PostedFile.SaveAs(picPath);
                        bool flag = false;
                        //最后一部高级验证，图片上传后的操作，判断是否真的是图片
                        StreamReader sr = new StreamReader(picPath, Encoding.Default);
                        string strContent = sr.ReadToEnd();
                        sr.Close();
                        string str = "request|script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=";
                        foreach (string s in str.Split('|'))
                            if (strContent.IndexOf(s) != -1)
                            {
                                File.Delete(picPath);
                                flag = true;
                                break;
                            }
                        if (!flag)
                        {
                            img.ImageUrl = "../images/" + SorH + "/" + type + "/" + txtname.Text + info + fileExtension;
                            img.Visible = true;
                            return "上传图片成功！";
                        }
                        else
                        {
                            return "图片格式非法！";
                        }
                    }
                    else
                    {
                        return "图片大小不能超过" + (size / 1000).ToString() + "K！";
                    }
                }
            }
            else
            {
                return "上传失败，格式不允许！";
            }
        }
        else
        {
            return "上传不能为空！";
        }
    }
}
